package com.xiaode.controller.app.notify;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.xiaode.common.constant.Const;
import com.xiaode.common.model.NotifyLog;
import com.xiaode.common.model.UserFaceLog;

import cn.hutool.core.bean.BeanUtil;

public class NotifyService {

	/**
	  * 通知未读数量
	  */
	public Ret noReadNotifyTotalNumber(Integer accountId) throws Exception {
		Integer count = Db.queryInt("SELECT COUNT(*) FROM notify_log WHERE account_id = ? AND read_state = 1", accountId);
		return Ret.ok("noReadNum", count);
	}
	
	/**
	 * 我的通知列表
	 */
	public Ret getNotifyList(Integer accountId, Integer userType, Integer page, Integer limit) throws Exception {
		Page<NotifyLog> notifyLogPage = NotifyLog.dao.paginate(page, limit, "SELECT *", "FROM notify_log WHERE account_id = ? ORDER BY id DESC", accountId);
		for(NotifyLog notifyLog : notifyLogPage.getList()) {
			if(notifyLog.getNotifyType() == Const.notifyType.FACE_EXAMINE) {
				UserFaceLog userFaceLog = UserFaceLog.dao.findFirst("SELECT state,reason,upload_time,examine_time FROM user_face_log WHERE id = ?", notifyLog.getEventId());
				notifyLog.put("userFaceLog", userFaceLog);
			}
			//TODO 以后还要加其他类型
		}
		return Ret.ok("notifyLogPage", notifyLogPage);
	}
	
	/**
	 * 阅读操作
	 */
	public Ret setRead(Integer accountId, Integer notifyLogId) throws Exception {
		NotifyLog notifyLog = NotifyLog.dao.findFirst("SELECT * FROM notify_log WHERE id = ? AND account_id = ?", notifyLogId, accountId);
		if(BeanUtil.isNotEmpty(notifyLog)) {
			notifyLog.setReadState(Const.faceNotifyReadType.READ);
			notifyLog.update();
		}
		return Ret.ok();
	}
	
	
	
	
}

